<template>
  <div class="conMain" >
    <el-card  class="box-card" >
      <div slot="header" class="topfix">
          <div style="text-align: right;">
            <el-button type="primary" size="small" class="ml10" @click="add" plain><i class="el-icon-plus" /> 添加</el-button>
          </div>
      </div>
      <el-table v-loading="loading"  :data="list" style="width: 100%" :empty-text='emtypetxt'>
          <el-table-column prop="id" label="ID" align="center" width="100" />
          <el-table-column prop="name" label="名称" align="center"  min-width="80"/>
          <el-table-column prop="appid" label="appid" align="center"  min-width="80"/>
          <el-table-column prop="agent_id" label="代理id" align="center"  min-width="80"/>
          <el-table-column prop="qrcode" label="二维码" align="center">
            <template slot-scope="scope">
              <el-image style="width:40px;height:40px" :src="scope.row.qrcode"
                :preview-src-list="[scope.row.qrcode]">
                <div slot="error" class="image-slot" />
              </el-image>
            </template>
          </el-table-column>

          <el-table-column prop="createtime" label="创建时间" align="center" width="200" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope" >
                <el-link  :underline="false" type="primary" title='编辑' @click="edit(scope.row)">编辑</el-link>
            </template>
          </el-table-column>
      </el-table>
      <div class="pagination">
          <el-pagination
            :current-page="params.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="params.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
      </div>
    </el-card>

    <el-dialog title="配置佣金"
    :visible.sync="showmb"
    :close-on-click-modal="false"
    left  width="60%" height="60%">
      <el-table v-loading="loadingmb"  :data="listmb" style="width: 100%" :empty-text='emptytxtmb'>
          <el-table-column prop="passage_name" label="所属渠道" align="center"  />
          <el-table-column prop="goods_id" label="商品ID" align="center" />
          <el-table-column prop="goods.name" label="商品名称" align="center" :show-overflow-tooltip="true"/>
          <el-table-column prop="goods.image" label="图片"  align="center" >
            <template slot-scope="scope">
              <el-image
                  style="width:40px;height:40px"
                  :src="scope.row.goods.image"
                  :preview-src-list="[scope.row.goods.image]"
              >
              <div slot="error" class="image-slot"></div>
              </el-image>
            </template>
          </el-table-column>

          <el-table-column prop="goods.fee" label="最高佣金" align="center"  min-width="80"/>
          <el-table-column prop="goods.type" label="类型" align="center"  >
            <template slot-scope="scope">
              <span v-if="scope.row.goods.type === 1">联通卡</span>
              <span v-else-if="scope.row.goods.type === 2">移动卡</span>
              <span v-else-if="scope.row.goods.type === 3">电信卡</span>
            </template>
          </el-table-column>
          <el-table-column prop="fee" label="模板佣金" align="center"  min-width="80"/>

          <el-table-column label="操作" fixed="right" align="center">
            <template slot-scope="scope" >
                <el-link  v-has="'passage/mubanfee/edit'"  :underline="false" type="primary" title='编辑' @click="mfEdit(scope.row.id)">编辑</el-link>
            </template>
          </el-table-column>
      </el-table>
      <div class="pagination">
          <el-pagination
            :current-page="paramsmb.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="paramsmb.pagesize"
            layout="total,  prev, pager, next"
            :total="totalmb"
            @current-change="handleCurrentChangemb"
          />
      </div>

    </el-dialog>


    </el-dialog>
    <el-dialog :title="sub.id==0?'添加':'编辑'" :visible.sync="dialogVisible" :close-on-click-modal="false" center
      width="40%">
      <el-form ref="ruleForm" :model="sub" :rules="rules" label-width="110px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="sub.name" clearable placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="appid" prop="appid">
          <el-input v-model="sub.appid" clearable placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="代理id" prop="agent_id">
          <el-input v-model="sub.agent_id" clearable placeholder="请输入代理id" />
        </el-form-item>
        <el-form-item label="二维码" prop="qrcode">
          <div class="upLoadPicBox" @click="modalPicTap('1')">
            <div v-if="sub.qrcode" class="upictrue"><img :src="sub.qrcode"></div>
            <div v-else class="upLoad">
              <i class="el-icon-camera cameraIconfont" />
            </div>
          </div>
        </el-form-item>
       </el-form >
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subForm('ruleForm')">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { muban_del,mubanfee_index,mubanfee_edit } from  '@/api/passage'
import { dy_index , dy_add ,dy_edit } from  '@/api/agent'
export default {
  data() {
    return {
      total:0,
      loading:true,
      emtypetxt:'',
      list: [],
      params: {
        page: 1,
        pagesize:10,
      },

      totalmb:0,
      loadingmb:false,
      emptytxtmb:'',
      listmb: [],
      paramsmb:{
        s_mb_id:'',
        page: 1,
        pagesize:10,
      },      
      sub: {
        id: '',
        name: '',
        appid: '',
        agent_id: '',
        qrcode: '',
      },
      rules: {
        name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }],
        appid: [{
          required: true,
          message: 'appid不能为空',
          trigger: 'blur'
        }],
        agent_id: [{
          required: true,
          message: '代理id不能为空',
          trigger: 'blur'
        }],
      },
      dialogVisible: false,
      showmb:false,

    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.emtypetxt='加载中...';
      this.loading=true;
      dy_index(this.params).then(res => {
        this.list=res.data.list
        this.total=res.data.total
        this.loading = false
        if(this.list.length==0){
          this.emtypetxt='暂无数据'
        }
      })
    },

    //分页
    handleSizeChange(val) {
        this.params.pagesize = val
        this.getList()
    },
    handleCurrentChange(val) {
        this.params.page = val
        this.getList()
    },
    add() {
      this.sub={
          id: '',
          name: '',
          appid: '',
          agent_id: '',
          qrcode: '',
      }
      this.dialogVisible = true
    },
    edit(row){
        this.sub={
          id: row.id,
          name: row.name,
          appid: row.appid,
          agent_id: row.agent_id,
          qrcode: row.qrcode,
      }
      this.dialogVisible = true
      // this.$modalForm(dy_add()).then(() => this.getList())
    },
    subForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.sub.id > 0) {
            dy_edit(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getList()
            })
          } else {
            dy_add(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getList()
            })
          }
        } else {
          return false
        }
      })
    },
    modalPicTap(tit, num, i) {
      const _this = this
      _this.$modalUpload(function(img) {
        if (tit === '1' && !num) {
          _this.sub.qrcode = img[0]
        }
      }, tit)
    },
    del(id) {
      let _this=this;
      _this.$modalSure().then(() => {
        muban_del({ids:id}).then(res => {
          _this.getList()
          _this.$message.success(res.msg)
        })
      })
    },

    //配置列表
    configlist(id){
      this.paramsmb.s_mb_id=id
      this.showmb=true
      this.getListmb()
    },
    getListmb() {
      this.emptytxtmb='加载中...';
      this.loadingmb=true;
      mubanfee_index(this.paramsmb).then(res => {
        this.listmb=res.data.list
        this.totalmb=res.data.total
        this.loadingmb = false
        if(this.listmb.length==0){
          this.emptytxtmb='暂无数据'
        }
      })
    },
    handleCurrentChangemb(val) {
        this.paramsmb.page = val
        this.getListmb()
    },
    mfEdit(id){
      this.$modalForm(mubanfee_edit({id:id})).then(() => this.getListmb())
    }

  },
}
</script>

<style lang="scss" scoped>


</style>
